﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;
using System.Collections;

namespace DCSLibrary
{
    public class ProductUI
    {

        public static string GetStatus(byte status)
        {
            switch (status)
            {
                case 0: return "Hết hàng";
                case 1: return "Sẵn hàng";
                case 2: return "Hàng đang về";
                case 3: return "Hàng đặt";
                default: return "";
            }
        }

        private const String CACHE_KEY = "Product_GetAll";

        public int Add(int CategoryPageID, String MetaKeyword, String MetaDescription, String ProductName, String ProductPageTitle, String ProductAlias, String ProductAbstract,
            String ProductContent, String ProductOther, String ProductImage, String ProductVideo, decimal OldPrice,
            decimal CurrentPrice, decimal DiscountPrice, decimal ImportPrice, decimal SellPrice, String Unit,
            String PromotionInformation, String Rate, String TransportInformation, String Note,int ViewTime, int ViewOrder,
            bool IsHot, bool IsTypical, bool IsNew, bool IsApproved, bool IsBestSeller, bool IsWeeklyProduct,
            bool IsPromotion, byte Status, int ApprovedBy, DateTime CreateDate, int CreateBy, String UpdateProcess, String ProductProperties)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Product_add", CategoryPageID,MetaKeyword, MetaDescription, ProductName,ProductPageTitle, ProductAlias, ProductAbstract,
            ProductContent, ProductOther,  ProductImage, ProductVideo, OldPrice,
             CurrentPrice,  DiscountPrice,  ImportPrice, SellPrice, Unit,
            PromotionInformation,  Rate,  TransportInformation,  Note, ViewTime, ViewOrder,
             IsHot,  IsTypical, IsNew,  IsApproved, IsBestSeller, IsWeeklyProduct,
             IsPromotion,  Status,  ApprovedBy, CreateDate, CreateBy, UpdateProcess, ProductProperties);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;

            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int ProductID, int CategoryPageID, String MetaKeyword, String MetaDescription, String ProductName, String ProductPageTitle, String ProductAlias, String ProductAbstract,
            String ProductContent, String ProductOther, String ProductImage, String ProductVideo, decimal OldPrice,
            decimal CurrentPrice, decimal DiscountPrice, decimal ImportPrice, decimal SellPrice, String Unit,
            String PromotionInformation, String Rate, String TransportInformation, String Note, int ViewOrder,
            bool IsHot, bool IsTypical, bool IsNew, bool IsBestSeller, bool IsWeeklyProduct,
            bool IsPromotion, byte Status, String UpdateProcess, String ProductProperties)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Product_update", ProductID, CategoryPageID, MetaKeyword, MetaDescription, ProductName, ProductPageTitle, ProductAlias, ProductAbstract,
            ProductContent, ProductOther, ProductImage, ProductVideo, OldPrice,
             CurrentPrice, DiscountPrice, ImportPrice, SellPrice, Unit,
            PromotionInformation, Rate, TransportInformation, Note, ViewOrder,
             IsHot, IsTypical, IsNew, IsBestSeller, IsWeeklyProduct,
             IsPromotion, Status, UpdateProcess, ProductProperties);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + ProductID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int UpdateRelateNews(int productID, string listNews)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "ProductNews_Update", productID, listNews);
                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int UpdateRelateNews(int productID, ArrayList listNews)
        {
            int result = 0;
            foreach (int newsid in listNews)
            {
                try
                {
                    int i = SqlHelper.ExecuteNonQuery(Common.Conn, "ProductNews_Update2", productID, newsid);
                    result++;
                }
                catch (Exception ex)
                {
                    
                }
            }
            return result;
        }

        public int UpdateView(int productID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Product_UpdateView", productID);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + productID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "Product_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet Search(string productName, int categoryID, double fromCurrentPrice, double toCurrentPrice, double fromDiscountPrice, double toDiscountPrice)
        {
            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "Product_Search", Utils.Format.ConvertToNoSign(productName), categoryID, fromCurrentPrice, toCurrentPrice, fromDiscountPrice, toDiscountPrice);
                return ds;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByCategoryPageID(int CategoryPageID)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "Product_GetByCategoryPageID", CategoryPageID); ;
                /*object obj = WebUtility.GetCacheObject(CACHE_KEY + "_Cate_" + CategoryPageID.ToString());
                if (obj == null)
                {
                    DataSet ds = 
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_Cate_" + CategoryPageID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }*/
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetRelateNews(int productID)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "ProductNews_GetByProduct", productID);
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID(int ProductID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + ProductID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "Product_get", ProductID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + ProductID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public int DeleteByID(int ProductID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Product_delete", ProductID);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        /// <summary>
        /// Cap nhat lai cac gia tri cho san pham
        /// </summary>
        /// <param name="ActionName">Ten Action, lay tu bien enum Utils.Common.ACTION_NAME</param>
        /// <param name="ProductListID">Danh sach cac productID, cach nhau bang dau phay</param>
        /// <returns>So ban ghi cap nhat thanh cong</returns>
        public int SetProperties(String ActionName, String ProductListID)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(Common.Conn, "Product_Action", ActionName, ProductListID);
            }
            catch (Exception e)
            {
                return -1;
            }
        }
    }
}
